assing-test

A short description of the post.

DONG Fang (Regina) https://www.linkedin.com/in/reginadongf/ (School of Computing and Information Systems, Singapore Management University)https://scis.smu.edu.sg/
07-17-2021

Launch packages

packages = c('ggiraph', 'plotly','DT', 'patchwork', 
             'raster', 'sf','tmap', 'mapview','gifski',
             'tidyverse', 'mlr','lubridate')
for (p in packages){
  if(!require(p, character.only = T)){
    install.packages(p)
  }
  library(p, character.only = T)
}

Import data

car_ass <- read_csv("data/car-assignments.csv")
gps <- read_csv("data/gps.csv")
cc <- read_csv("data/cc_data.csv", locale = locale(encoding = "windows-1252"))
loyalty <- read_csv("data/loyalty_data.csv", locale = locale(encoding = "windows-1252"))

gps$Timestamp = mdy_hms(gps$Timestamp)
cc$timestamp = mdy_hm(cc$timestamp)
loyalty$timestamp = mdy(loyalty$timestamp)

car_ass$CarID = as.character(car_ass$CarID)
gps$id = as.character(gps$id)
cc$last4ccnum = as.character(cc$last4ccnum)

cc$day = day(cc$timestamp)
cc$hour = hour(cc$timestamp)
loyalty$day = day(loyalty$timestamp)
gps$day = as.factor(day(gps$Timestamp))
gps$hour = as.factor(hour(gps$Timestamp))

Interactive plot

d <- highlight_key(cc)

gra_1 <- plot_ly(data = d, x = ~as.factor(hour), y = ~location,
                 hovertemplate = paste(
                   " %{yaxis.title.text}: %{y}<br>",
                   "%{xaxis.title.text}: %{x}<br>",
                   "Transaction Count: %{z}",
                   "<extra></extra>")) %>%
  add_histogram2d(colors = "Blues") %>%
  layout(title = "<b>Graph.1 Credit Card Transcation Frequency by Hour</b>",
         xaxis = list(title = "Time", tickmode = "linear"),
         yaxis = list(title="Location", tickmode = "linear")
         )

crosstalk::bscols(gra_1,
                  crosstalk::filter_select("day", "Day", d, ~as.factor(day), multiple = F), widths = 10)

Map

gra_2.1 <- plot_ly(data = cc, x = ~as.factor(day), y = ~location,
                 hovertemplate = paste(
                   " %{yaxis.title.text}: %{y}<br>",
                   "%{xaxis.title.text}: %{x}<br>",
                   "Transaction Count: %{z}",
                   "<extra></extra>")) %>%
  add_histogram2d(colors = "Blues") %>%
  layout(annotations = list(text = "Credit Card", showarrow = F, x =10, y=32),
         xaxis = list(tickmode = "linear"),
         yaxis = list(tickmode = "linear")
         )

gra_2.2 <- plot_ly(data = loyalty, x = ~as.factor(day), y = ~location,
                 hovertemplate = paste(
                   " Location: %{y}<br>",
                   "Date of Jan: %{x}<br>",
                   "Transaction Count: %{z}",
                   "<extra></extra>")) %>%
  add_histogram2d(colors = "Purples") %>%
  layout(annotations = list(text = "Loyalty Card", showarrow = F, x =10, y=32),
         xaxis = list(tickmode = "linear"),
         yaxis = list(tickmode = "linear", visible = T)
         )

gra_2 <- subplot(gra_2.1, gra_2.2, nrows = 1, shareY = T) %>%
  layout(title = "<b>Graph.2 Transaction Frequency by Day</b>",
         xaxis = list(title = "Date of Jan"),
         xaxis2 = list(title = "Date of Jan"),
         yaxis = list(title = "Location"),
         autosize = F, width = 900, width2 = 900, height = 400
         )

gra_2

geo map

bgmap <- raster("data/Geospatial/MC2-tourist.tif")

tm_shape(bgmap) +
tm_rgb(bgmap, r = 1,g = 2,b = 3,
       alpha = NA,
       saturation = 1,
       interpolate = TRUE,
       max.value = 255)
Abila_st <- st_read(dsn = "data/Geospatial",layer = "Abila")
Reading layer `Abila' from data source 
  `D:\ReginaDong\DataViz_blog\_posts\2021-07-17-assing-test\data\Geospatial' 
  using driver `ESRI Shapefile'
Simple feature collection with 3290 features and 9 fields
Geometry type: LINESTRING
Dimension:     XY
Bounding box:  xmin: 24.82401 ymin: 36.04502 xmax: 24.90997 ymax: 36.09492
Geodetic CRS:  WGS 84
gps_sf <- st_as_sf(gps, 
                   coords = c("long", "lat"),
                   crs = 4326)

# Group by id and day
gps_path <- gps_sf %>%
  group_by(id, day) %>%
  summarize(m = mean(Timestamp), 
            do_union=FALSE) %>%
  st_cast("LINESTRING")

np = npts(gps_path, by_feature = T)
gps_path2 <- cbind(gps_path, np) %>%
  filter(np > 1) # exclude orphan coordinate records

# Group by day and hour
gps_hour <- gps_sf %>%
  group_by(day, hour) %>%
  summarise(m = mean(Timestamp),
            do_union = FALSE) %>%
  st_cast("LINESTRING")
tmap_mode("view")

m <- tm_shape(bgmap) +
  tm_rgb(bgmap, r = 1,g = 2,b = 3,
       alpha = NA,
       saturation = 1,
       interpolate = TRUE,
       max.value = 255) +
  tm_shape(gps_path2) +
  tm_lines(col = "day") 
  #tm_facets(by = "id")
m